package com.example.justbuy.action.buyer;

import com.example.justbuy.JustbuyApplication;
import com.example.justbuy.pojo.User;
import com.example.justbuy.service.OrderMapperService;
import com.example.justbuy.service.UserDaoService;
import com.example.justbuy.test.SmsDemo;
import com.example.justbuy.util.ConvertDateUtil;
import com.example.justbuy.util.MySqlMD5Until;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

@Controller
public class registAction {

    @Autowired
    private UserDaoService userDaoService;
    @Autowired
    private OrderMapperService orderMapperService;
    /**
     * 注册页面跳转
     *@return
     */
    @RequestMapping("/regist.htm")
    public String regist(){
        return "regist.html";
    }
    /**
     * 协议页面跳转
     * @return
     */
    @RequestMapping("/xieyi.htm")
    public String xieyi(){
        return "xieyi.html";
    }

    /**
     * 注册跳转
     * @return
     */
    @RequestMapping("/registl.htm")
    public String registl(String phone, String password, Model md,HttpServletRequest request, HttpServletResponse response){
        password= MySqlMD5Until.javamd5jm(password);
        HashMap map=new HashMap();
        map.put("phone",phone);
        map.put("password",password);
        map.put("roleid",Integer.valueOf(1));
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
        map.put("addTime",ConvertDateUtil.castDate(date));
       int flag= userDaoService.insertUser(map);
        List<User> user= userDaoService.getByPhone(phone);
       if(flag>0){
           request.getSession().setAttribute("user",user.get(0));
       }
        return "redirect:/";
    }

    /**
     * 登录页面跳转
     * @return
     */
    @RequestMapping("/login.htm")
    public String login(){
        return "login.html";
    }


    /**
     * 登录处理页面
     * @return
     */
    @RequestMapping("/login01.htm")
    public@ResponseBody String login01(String phone,String password,HttpServletRequest request, HttpServletResponse response){
        password=MySqlMD5Until.javamd5jm(password);
        HashMap map=new HashMap();
        map.put("phone",phone);
        map.put("password",password);
        map.put("roleid",Integer.valueOf(1));
        User user= userDaoService.validateUser(map);
        if(user!=null){
            request.getSession().setAttribute("user",user);
            return "1";
        }else{
            map.put("roleid",Integer.valueOf(2));
            user= userDaoService.validateUser(map);
            if(user!=null) {
                request.getSession().setAttribute("user", user);
                return "1";
            }else{
                return  "2";
            }
        }
    }


    /**
     * 注册用户判断是否存在页面跳转
     * @return
     */
    @RequestMapping("/ceshi.htm")
    public@ResponseBody String cesi(String phone)
    {
        List<User> list=userDaoService.getByPhone(phone);
        if(list.size()>0){
            return "1";
        }else{
            return "2";
        }
    }

    /**
     * 短信验证码页面跳转
     * @return
     */
    @RequestMapping("/duanxin.htm")
    public@ResponseBody String duanxin(String phone) throws Exception
    {
        Random random = new Random();
        int dd= random.nextInt(999999);
        if(dd<100000){
            dd=dd+100000;
        }
        String xcode=String.valueOf(dd);
           // SmsDemo.sendSms(phone,xcode);
       boolean flag= SmsDemo.callback(phone,xcode);
       if(flag){
           return  xcode;
       }else{
           return  "1";
       }
    }
    /**
     * 找回密码短信验证码页面跳转
     * @return
     */
    @RequestMapping("/duanxin01.htm")
    public@ResponseBody String duanxin01(String phone) throws Exception
    {
        Random random = new Random();
        int dd= random.nextInt(999999);
        if(dd<100000){
            dd=dd+100000;
        }
        String xcode=String.valueOf(dd);
        //SmsDemo.sendSms01(phone,xcode);
        boolean flag= SmsDemo.callback01(phone,xcode);
        if(flag){
            return  xcode;
        }else{
            return  "1";
        }
    }
    /*
    * 找回密码
    * */
    @RequestMapping("/psd_change.htm")
    public@ResponseBody String psd_change(String phone, String password, Model md,HttpServletRequest request){
        password= MySqlMD5Until.javamd5jm(password);
        HashMap map=new HashMap();
        map.put("phone",phone);
        map.put("password",password);
        int flag=userDaoService.uadateUser(map);
        if(flag>0){
            request.getSession().removeAttribute("user");
            return "1";
        }else{
            return "2";
        }

    }
    /**
     * 找回密码首页跳转
     * @return
     */
    @RequestMapping("/psd_chage.htm")
    public String psd_chage(){
        return "psd_chage.html";
    }
    /**
     * 个人中心跳转
     * @return
     */
    @RequestMapping("/myHome.htm")
    public String myHome(HttpServletRequest request,Model md){
        User user=(User)request.getSession().getAttribute("user");
        if(user!=null){
            //获取对应userid的对应信息
            User us=userDaoService.getTheInfoUser(user.getId());

            //获取订单表中的数量
            HashMap map1=new HashMap();
            map1.put("userId",user.getId());
            map1.put("orderState","待付款");
            Integer number1=orderMapperService.getSum0fOrderByState(map1);

            HashMap map2=new HashMap();
            map2.put("userId",user.getId());
            map2.put("orderState","已付款");
            Integer number2=orderMapperService.getSum0fOrderByState(map2);

            HashMap map3=new HashMap();
            map3.put("userId",user.getId());
            map3.put("orderState","待收货");
            Integer number3=orderMapperService.getSum0fOrderByState(map3);

            HashMap map4=new HashMap();
            map4.put("userId",user.getId());
            map4.put("orderState","待评价");
            Integer number4=orderMapperService.getSum0fOrderByState(map4);

            md.addAttribute("us",us);
            md.addAttribute("number1",number1);
            md.addAttribute("number2",number2);
            md.addAttribute("number3",number3);
            md.addAttribute("number4",number4);
            return "mine.html";
        }else{
            return "login.html";
        }

    }
    /**
     * 注销账号跳转
     * @return
     */
    @RequestMapping("/exit.htm")
    public void exit(HttpServletRequest request,Model md, HttpServletResponse response)throws  Exception{
        request.getSession().removeAttribute("user");
        response.sendRedirect("/");
    }

}
